package com.tuanzi.loan.web.bean.product.space;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.product.space.LoanProductSpaceService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.product.space.LoanProductSpaceSearchRequest;
import com.tuanzi.loan.vo.response.product.space.LoanProductSpaceSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.product.space.LoanProductSpaceLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.LARGE_COMMON_PRODUCT;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanProductSpaceListBean extends JSFListingSessionBean<LoanProductSpaceSearchRequest, LoanProductSpaceSearchResponse> {

	private static final long serialVersionUID = -5164434375302389573L;

	@Value("${family.topic.url.template}")
	private String topicUrlTemplate;

	@Autowired
	private LoanProductSpaceService loanProductSpaceService;

	@Override
	protected String page() {
		return "/views/product/space/list.xhtml";
	}

	@Override
	protected void initLazyDataModel() {
		$ = new LoanProductSpaceSearchRequest();
		lazyDataModel = new LoanProductSpaceLazyDataModel($, session, loanProductSpaceService);
	}

	public String onAdd() {
		return redirect("/views/product/space/info.xhtml");
	}

	public String onEdit(String id) {
		return redirect("/views/product/space/info.xhtml?id=" + id);
	}

	public void onReset() {
		initLazyDataModel();
	}

	public String translateTopicUrl(Integer type, Long id) {
		if (null == type || 2 != type) {
			return "";
		}
		return topicUrlTemplate + id;
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(LARGE_COMMON_PRODUCT);
	}
}
